/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RMS;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * @author SamSung
 */
public class RememberLoadedLevel {

    private static RecordStore rs = null;

    public static void createRecord(int level) {
        try {
            rs = RecordStore.openRecordStore("level", true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream data = new DataOutputStream(baos);
        try {
            data.writeInt(level);
            data.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        System.out.println("level=" + level);
        byte[] rec = baos.toByteArray();

        try {
            rs.addRecord(rec, 0, rec.length);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

        try {
            data.close();
            baos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            rs.closeRecordStore();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    public static int getRecord() {
        try {
            rs = RecordStore.openRecordStore("level", true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        RecordEnumeration re = null;
        int levelLoaded = 0;
        try {
            re = rs.enumerateRecords(null, new IntergerCompare(), true);
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }
        if (re.hasNextElement()) {
            try {
                byte[] dt = re.nextRecord();
                DataInputStream dis = new DataInputStream(new ByteArrayInputStream(dt));
                try {
                    levelLoaded = dis.readInt();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            } catch (InvalidRecordIDException ex) {
                ex.printStackTrace();
            } catch (RecordStoreNotOpenException ex) {
                ex.printStackTrace();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        } else {
            try {
                rs.closeRecordStore();
            } catch (RecordStoreNotOpenException ex) {
                ex.printStackTrace();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
            return 0;
        }
        try {
            rs.closeRecordStore();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        return levelLoaded;
    }
}

class IntergerCompare implements RecordComparator {

    public int compare(byte[] rec1, byte[] rec2) {
        int i1 = 0, i2 = 0;
        DataInputStream is1 = new DataInputStream(new ByteArrayInputStream(rec1));
        DataInputStream is2 = new DataInputStream(new ByteArrayInputStream(rec2));

        try {
            i1 = is1.readInt();
            i2 = is2.readInt();
        } catch (EOFException e) {
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        if (i1 > i2) {
            return RecordComparator.PRECEDES;
        }
        if (i1 < i2) {
            return RecordComparator.FOLLOWS;
        }
        return RecordComparator.EQUIVALENT;
    }
}
